package redis

import (
	"github.com/astaxie/beego"
	"github.com/gomodule/redigo/redis"
	"time"
)

//redis前缀
const Prefix = "getshop."

var Pool *redis.Pool

func init() {
	//获取redis配置
	host := beego.AppConfig.String("redis.host")
	pass := beego.AppConfig.String("redis.pass")
	db, _ := beego.AppConfig.Int("redis.db")
	//建立连接池
	Pool = &redis.Pool{
		MaxIdle:     beego.AppConfig.DefaultInt("redis.maxidle", 10),
		MaxActive:   beego.AppConfig.DefaultInt("redis.maxactive", 20),
		IdleTimeout: 180 * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", host)
			if err != nil {
				return nil, err
			}
			//登录密码
			if pass != "" {
				if _, err := c.Do("AUTH", pass); err != nil {
					c.Close()
					return nil, err
				}
			}
			//选择db
			c.Do("SELECT", db)
			return c, nil
		},
	}
}
